﻿<#
.SYNOPSIS
	This script simulates the behavior of NUnit4PowerShell.dll
	It has been done to be able to debug inside the functions to test
#>
[CmdletBinding()]
param
(
	[string] $Filter = "*",
	[string] $Include = "",
	[string] $Exclude = "",
	[switch] $WhatIf
)

# This script path
Set-Variable -Name SCRIPT_LOCATION_PATH -Value (Split-Path (Resolve-Path $myInvocation.MyCommand.Path)) -Scope local

# NUnit4PowerShell config file name
Set-Variable -Name NUNIT4POWERSHELL_DLL_NAME -Value "NUnit4PowerShell.dll"
# NUnit4PowerShell config file name
Set-Variable -Name NUNIT4POWERSHELL_DLL_PATH -Value "$SCRIPT_LOCATION_PATH\..\$NUNIT4POWERSHELL_DLL_NAME"

function Load-NUnit4PowerShell
{
<#
	.SYNOPSIS
Load NUnit4PowerShell DLL
#>
	[CmdletBinding()]
	param()

	if (!(Test-Path $NUNIT4POWERSHELL_DLL_PATH))
	{
		throw "NUnit4PowerShell DLL has not been found at the following path : '$NUNIT4POWERSHELL_DLL_PATH'"
	}
	$dllLoading = [System.Reflection.Assembly]::LoadFile($NUNIT4POWERSHELL_DLL_PATH)
	Write-Verbose $dllLoading
}

function Execute-FixtureFunction
{
<#
.SYNOPSIS
	Execute a PowerShell containing a NUnit4PowerShell Fixture
#>
	[CmdletBinding()]
	param
	(
		[string] $Name,
		[switch] $WhatIf
	)
	
	$function = Get-Item function:$Name -ErrorAction SilentlyContinue
	if ($function)
	{
		if ($WhatIf)
		{
			Write-Host "What if: Performing operation `"Execute FixtureFunction`" on Target `"Item: $Name`"".
			return
		}
		Invoke-Expression -Command  "$function" | Out-Null
	}
	
}

function Execute-PSTestFixture
{
<#
.SYNOPSIS
	Execute a PowerShell containing a NUnit4PowerShell Fixture
#>
	[CmdletBinding()]
	param
	(
		[string] $Path,
		[string] $Filter = "*",
		[string] $Include = "*",
		[string] $Exclude = "",
		[switch] $WhatIf
	)
	
	$moduleName = [System.IO.Path]::GetFileNameWithoutExtension($Path)
	Import-Module $Path -Force -DisableNameChecking

	Execute-FixtureFunction -Name "SetUp-TestFixture" -WhatIf:$WhatIf

	# Get the list of functions contains inside the fixture
	$functionsToTest = Get-Item function:$testFunctionNamePattern -Include $Include -Exclude $Exclude | Sort-Object -Property "Name"
	$functionsToTest = ($functionsToTest | Where-Object { $_.Name -like $Filter })
	
	foreach ($functionToTest in $functionsToTest)
	{
		Execute-FixtureFunction -Name "SetUp-Test" -WhatIf:$WhatIf

		if ($WhatIf)
		{
			Write-Host  "What if: Performing operation `"Execute Function`" on Target `"Item: $functionToTest`"".
		}
		else
		{
			try
			{
				Invoke-Expression -Command  "$functionToTest" | Out-Null
				Write-Host "[Execute-Test] [OK]    $functionToTest"
			}
			catch [Exception]
			{
				Write-Host ("[Execute-Test] [ERROR] $functionToTest " + $_.Exception.ToString())
				continue
			}
		}
		Execute-FixtureFunction -Name "TearDown-Test" -WhatIf:$WhatIf
	}
	
	Execute-FixtureFunction -Name "TearDown-TestFixture" -WhatIf:$WhatIf
	
	Remove-Module -Name $moduleName -Force
}


#--------------------------------------------------------------------------------------------------------
# BEGINNING OF THE SCRIPT
#--------------------------------------------------------------------------------------------------------



Load-NUnit4PowerShell

$folderPath = [NUnit4PowerShell.Utils]::GetFolderPath() # Doesn't work correctly here : so far you still have to specify the test folder
$folderPath = Resolve-Path "$SCRIPT_LOCATION_PATH\..\.."
$script:testFunctionNamePattern = [NUnit4PowerShell.Utils]::GetPSTestFunctionNamePattern()
$fixtureNamePattern = [NUnit4PowerShell.Utils]::GetPSFixtureNamePattern()

Write-Verbose "The path of the folder containing the script is : '$folderPath'"
	
Write-Host "[Execute-Test] Launch tests"

Set-Location -Path $folderPath

$fixturePSFileList = Get-ChildItem -Path $folderPath -Filter $fixtureNamePattern

foreach($fixture in $fixturePSFileList)
{
	Execute-PSTestFixture -Path $fixture.FullName -Include $Include -Exclude $Exclude -WhatIf:$WhatIf
}

Write-Host "[Execute-Test] Tests end"
